package net.cyndeline.components.gameplay

import net.cyndeline.entitySystem.Component
import net.cyndeline.math.Rational
import com.escalatesoft.subcut.inject.BindingModule
import com.escalatesoft.subcut.inject.Injectable
import net.cyndeline.subcut.BindingKeys.DefaultMoveTime

/**
 * Stores how much time an entity has left until it applies
 * some sort of state-altering effect on the game world (the most
 * common would be creatures performing an action).
 * 
 * Time is counted down to zero, at which point something is supposed
 * to happen (this should be taken care of by other systems).
 */
trait TimeComponent extends Component {
  
  /** How much time is left on the component. */
  def timeLeft: Rational
  
  /** Modifies the time left by adding the amount. */
  def modify(amount: Rational): Rational
  
  /** 
   *  Resets the amount to a set value. Only use this to reset the time
   *  after an action has been completed, not to modify it.
   */
  def reset(value: Rational): Rational
}

class Time()(implicit val bindingModule: BindingModule) extends TimeComponent with Injectable {
  private var time: Rational = injectOptional [Rational](DefaultMoveTime) getOrElse { 100 }

  override def timeLeft = time
  
  override def modify(amount: Rational) = {
    time = time + amount
    time
  }
  
  override def reset(value: Rational) = {
    time = value
    time
  }
  
  override  def copy = {
    val copy = new Time()
    copy.reset(time)
    copy
  }
}